<?php
$document_title = 'FakeBank :: PHP + DB';
$document_meta_description = '<meta name="description" content="PHP DB binding examples for fake bank page content">';
$breadcrumbs = 'FakeBank PHP DB Binding';
include "fakebank_header.html"; ?>

<!-- Set up the nav buttons how you like for each page. Specifically, the 'selected' class. 
		Leave start and end classes on first and last buttons. -->
	<nav>
    	<ul>
        	<li class="start"><a href="index.php">Home</a></li>
            <li><a href="styling.php">Styling Examples</a></li>
            <li><a href="template.php">Template page</a></li>
            <li class="selected"><a href="php_binding.php">PHP DB Binding</a></li>
            <li class="end"><a href="#">Contact Us</a></li>
        </ul>
    </nav>

<!-- Breadcrumb trail -->
<div id="breadcrumbs"><a href="index.php">FakeBank</a> <?php if (isset ($breadcrumbs))echo ' > '.$breadcrumbs; ?> </div>


    <div id="body">
        <section id="content">
	    <article>
	    
		<h1>PHP + MySQL calls</h1>
		
		<h2>Step 1 is to connect to the database you want.</h2>
		<p>The thing about php is that it's server-side code. That means its output gets rendered to HTML, and it "does stuff", but you don't see a php file's whole contents just by visiting a php page. Instead, you see what the php produces/renders in terms of that HTML output. So this means you need to look at the actual php_binding.php file to see how this is being done, since you won't be able to view it from a browser.</p>
		
		<p>Let's just use a simple example since I have to leave and run some errands soon. Let's use the same "world" DB from the document I uploaded to my SVN folder (<a href="http://uah-cs499-group1.googlecode.com/svn/sflynn/Setting_up_MySQL_locally.docx" alt="document in my branch of repository">this</a> document). We already have a stored procedure in the MySQL database from following along with that document that we made about population retrieval. Let's make a php call to that procedure! Yay, cities!</p>
		
		<p>Let's set up the php we need for this. Here's some <a href="http://php.net/manual/en/pdo.connections.php" alt="php manual">suggested reading</a>. I'm stepping out of HTML below and starting php, though I'm still in the &lt;article&gt; section of the HTML document. You can't see what I'm doing now with the php unless you look at the php_binding.php file, with the exception of rendering output.</p>
		
		<?php
			/*BLANK FOR SECURITY REASONS w/svn.
			However, the values you put here should look like this. Let me know if you have problems.
			Note we have one single quote at the start and end; this 1st one is all 1 string:
			$dbname = 'mysql:host=this-is-probably-localhost;dbname=put-name-of-db-to-use-here';
			$user = 'username-for-db-access';	
			$pass = 'secret-password-for-db';*/
			$dbname = '';
			$user = '';	
			$pass = '';		
		
		//TRY to connect
		try
		{
			$dbhandle = new PDO($dbname, $user, $pass);
			echo "<br> Connect message: no problem. closing connection.<br><br>";
			
			$dbhandle = null;	//Kill connection when finished with it.
		}
		
		//Catch errors, but mind what you display on a public webpage concerning them.
		catch(PDOException $e)
		{
			echo "DB Connection error. Error details not exposed on this webpage.";
			
			/*Uncomment this line if you want to see the error. Not a great idea
			for a live webpage but useful while building stuff:
			echo  $e->getMessage();*/
			
			echo "<br>Dying on failed connect. Remainder of page will fail to render.";
			die();
		}
		//Remember to close php tag so you can get on with the HTML again
		?>
		
		<h2>Step 2: Now that we can connect, DO something with the connection</h2>
		<p>Not very exciting in terms of "doing stuff", but that's ok, we'll do that now. Feel free to mess around with causing a connection error on purpose above.</p>
		
		<p>Now that we're back in HTML-land, let's ponder this. We want the user to give us input, so we think about putting an &lt;input&gt; box on our page somewhere. OK, fine. Now, we can get info from client to server (specifically to php) with basically 2 methods, GET or POST.</p>
		
		<p>GET is very easy. It adds input to the URL. For example, https://www.google.com/search?q=words is what I get when I search for 'words' with Google. Not the brightest idea for a Fake Banking site.</p>
		
		<p>POST is a little different because POST conceals the info being sent between the client and server. On the plus side, this makes us happy for our project. On the downside, users can't bookmark pages using POST because the URL isn't being modified. Let's use it anyway.</p>
		
		<p>Make a form in HTML (<a href="http://www.w3schools.com/php/php_forms.asp" alt="w3c link">reference #1</a>, <a href="http://www.w3schools.com/php/php_forms.asp" alt="w3c link">reference #2</a>)</p>
		
		<form id="population-input-form" action="city-population.php" method="post">
		Show me cities with &gt; <input type="text" name="population-form-input"> people<br>
		<strong>When you click this button (or press enter) you will go to a new page.</strong> This is because you are sending info to the server for server processing, so the server must...process...the info and then your client will need to load the result. There are ways to reload just part of a webpage, of course; this is done by using javascript, which I don't have time to get into today, but I will try to include an example of that "Soon".<br>
		<input type="submit">
		</form>
		
		</article>
        </section>
        
        
    	<div class="clear"></div>
		</div> <!--end body div -->
		
<?php include "fakebank_footer.html"; ?>		